package com.example.covid.controller;

import com.example.covid.Utils.Result;
import com.example.covid.Vo.InfQuery;
import com.example.covid.pojo.Area;
import com.example.covid.pojo.InfectedPersonInformation;
import com.example.covid.service.InfectedPersonInformationService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/** @author KamenRider */
@Controller
public class InfectedPersonInformationController {
  @Resource private InfectedPersonInformationService infectedService;

  /**
   * 欢迎页面
   *
   * @return
   */
  @GetMapping("/welcome")
  public String welcome() {
    return "welcome";
  }

  /**
   * 数据页面详情
   *
   * @return
   */
  @RequestMapping("InfList")
  public String InfListUi() {
    return "InfList";
  }

  /**
   * 添加页面   获取所有区名称
   * @return
   */
  @RequestMapping("getAllAreaName")
  public String getAreaName(Model model) {
    List<Area> allAreaName = infectedService.getAllAreaName();
    model.addAttribute("areaList",allAreaName);
    return "addUi";
  }

  /**
   * 新增
   * @param information
   * @return
   */
  @RequestMapping("addInf")
  @ResponseBody
  public Result<Object> addInf(InfectedPersonInformation information){
    infectedService.addInf(information);
    return Result.success("新增信息成功");
  }

  /**
   * 分页 获取全部数据 模糊查询
   *
   * @return
   */
  @RequestMapping("getAllInfo")
  @ResponseBody
  public Result<Object> getInfList(InfQuery infQuery) {
    List<InfectedPersonInformation> informationList = infectedService.getAllInfo(infQuery);
    Long count = infectedService.countInfList(infQuery);
    return Result.success(informationList, count);
  }

  /**
   * 删除
   * @param ids  传入多个ID值
   * @return
   */
  @DeleteMapping("del/{ids}")
  @ResponseBody
  public Result<Object> deleteInfByIds(@PathVariable("ids") String ids){
    infectedService.deleteInfByIds(ids);
    return Result.success("删除数据成功");
  }

  /**
   * 根据id获取数据
   * @param id
   * @return
   */
  @RequestMapping("update/{id}")
  public String getInfById(@PathVariable("id") Integer id,Model model){
    InfectedPersonInformation information = infectedService.getInfById(id);
    model.addAttribute("Inf",information);
    model.addAttribute("areaList",infectedService.getAllAreaName());
    return "infEdit";
  }

  @RequestMapping("updateInf")
  @ResponseBody
  public Result<Object> updateInf(InfectedPersonInformation information){
    infectedService.updateInf(information);
    return Result.success("信息修改成功");
  }
}
